package main

import (
	"bytes"
	"fmt"
	"html/template"
	"time"
)

var fns = template.FuncMap{
	"unescaped":  func(str string) interface{} { return template.HTML(str) },
	"urlconvert": func(str string) interface{} { return template.URL(str) },
	"timeformat": func(ts int64, pattern ...string) string {
		defp := "2006-01-02 15:04:05"
		if len(pattern) > 0 {
			defp = pattern[0]
		}
		return time.Unix(ts, 0).Format(defp)
	},
	"timestamp": func(pattern ...string) string {
		defp := "2006-01-02 15:04:05"
		if len(pattern) > 0 {
			defp = pattern[0]
		}
		return time.Now().Format(defp)
	},
}

type UserInfo struct {
	Name   string
	Gender string
	Age    int
}

func main() {
	tmpl, _ := template.New("hello.tpl").Funcs(fns).ParseFiles("./hello.tpl")
	user := UserInfo{
		Name:   "zhanglin",
		Gender: "男",
		Age:    18,
	}
	var body bytes.Buffer
	tmpl.Execute(&body, user)
	fmt.Println(body.String())

}
